package com.nbsaas.boot.solution.data.entity;

import com.nbsaas.boot.ad.data.entity.AdPosition;
import com.nbsaas.boot.code.annotation.*;
import com.nbsaas.boot.jpa.data.entity.CatalogEntity;
import com.nbsaas.boot.rest.filter.Operator;
import lombok.Data;
import org.hibernate.annotations.Comment;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;


@SearchBean(
        items = {
                @SearchItem(label = "深度", name = "depth", operator = Operator.eq, show = false, classType = Integer.class),
                @SearchItem(label = "名称", name = "name", operator = Operator.like, show = false),
                @SearchItem(label = "编码", name = "code", operator = Operator.like, show = false),
                @SearchItem(label = "ids", name = "ids", operator = Operator.like, show = false),
        }
)
@org.hibernate.annotations.Table(appliesTo = "bs_solution_catalog", comment = "解决方案分类")
@Data
@FormAnnotation(title = "解决方案分类", model = "解决方案分类")
@Entity
@Table(name = "bs_solution_catalog")
@CatalogClass
public class SolutionCatalog extends CatalogEntity {


    @SearchItem(label = "父级", name = "parent", key = "parent.id", operator = Operator.eq, show = false, classType = Long.class)
    @FieldName
    @FieldConvert
    @ManyToOne(fetch = FetchType.LAZY)
    private SolutionCatalog parent;

    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
    private List<SolutionCatalog> children;

    @FormField(title = "分类图标", type = InputType.image)
    private String logo;

    @SearchItem(label = "广告位", name = "adPosition", key = "adPosition.id", operator = Operator.eq, show = false, classType = Long.class)
    @FieldName
    @FieldConvert
    @ManyToOne(fetch = FetchType.LAZY)
    private AdPosition adPosition;

    @SearchItem(label = "手机端广告位", name = "mobile", key = "mobile.id", operator = Operator.eq, show = false, classType = Long.class)
    @FieldName
    @FieldConvert
    @ManyToOne(fetch = FetchType.LAZY)
    private AdPosition mobile;

    @Comment("解决方案标题")
    private String title;

    @Comment("解决方案图片")
    private String solutionLog;

    @Comment("解决方案介绍")
    private String note;


    @Override
    public Serializable getParentId() {
        if (parent != null) {
            return parent.getId();
        }
        return null;
    }
}
